package health

import (
	"gitcode.com/G-YT/iHealth25/server/global"
	"gitcode.com/G-YT/iHealth25/server/model/common/request"
	"gitcode.com/G-YT/iHealth25/server/model/health"
	healthReq "gitcode.com/G-YT/iHealth25/server/model/health/request"
	"gorm.io/gorm"
)

type DiseaseService struct {
}

// CreateDisease 创建Disease记录
// Author [piexlmax](https://github.com/piexlmax)
func (diseaseService *DiseaseService) CreateDisease(disease *health.Disease) (err error) {
	err = global.GVA_DB.Create(disease).Error
	return err
}

// DeleteDisease 删除Disease记录
// Author [piexlmax](https://github.com/piexlmax)
func (diseaseService *DiseaseService) DeleteDisease(disease health.Disease, isDisease string) (err error) {
	if isDisease == "1" {
		err = global.GVA_DB.Delete(&health.Disease{}, "id = ?", disease.ID).Error
	} else {
		err = global.GVA_DB.Delete(&health.DiseaseList{}, "id = ?", disease.ID).Error
	}
	return err
}

// DeleteDiseaseByIds 批量删除Disease记录
// Author [piexlmax](https://github.com/piexlmax)
func (diseaseService *DiseaseService) DeleteDiseaseByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]health.Disease{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateDisease 更新Disease记录
// Author [piexlmax](https://github.com/piexlmax)
func (diseaseService *DiseaseService) UpdateDisease(disease health.Disease) (err error) {
	err = global.GVA_DB.Save(&disease).Error
	return err
}

// GetDisease 根据id获取Disease记录
// Author [piexlmax](https://github.com/piexlmax)
func (diseaseService *DiseaseService) GetDisease(id uint, isDisease string) (d any, err error) {
	var diseaseList health.DiseaseList
	var disease health.Disease
	if isDisease == "1" {
		global.GVA_DB.Where("id = ?", id).First(&disease)
		d = disease
	} else {
		global.GVA_DB.Where("id = ?", id).First(&diseaseList)
		d = diseaseList
	}
	return
}

// GetDiseaseInfoList 分页获取Disease记录
// Author [piexlmax](https://github.com/piexlmax)
func (diseaseService *DiseaseService) GetDiseaseInfoList(info healthReq.DiseaseSearch) (list []health.DiseaseList, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&health.DiseaseList{}).Preload("Diseases")
	var diseases []health.DiseaseList
	// 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&diseases).Error
	return diseases, total, err
}

func (diseaseService *DiseaseService) SetDiseaseGraphic(req healthReq.DiseaseGraphicReq) (err error) {
	var DiseaseGraphic []healthReq.DiseaseArticle
	return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		err = tx.Where("disease_id = ?", req.DiseaseID).Delete(&DiseaseGraphic).Error
		if err != nil {
			return err
		}
		var diseaseGraphic []healthReq.DiseaseArticle
		for _, graphic := range req.Graphics {
			diseaseGraphic = append(diseaseGraphic, healthReq.DiseaseArticle{
				DiseaseID: req.DiseaseID,
				ArticleID: graphic,
				Type:      "graphic",
			})
		}
		for _, audio := range req.Audios {
			diseaseGraphic = append(diseaseGraphic, healthReq.DiseaseArticle{
				DiseaseID: req.DiseaseID,
				ArticleID: audio,
				Type:      "audio",
			})
		}
		for _, video := range req.Videos {
			diseaseGraphic = append(diseaseGraphic, healthReq.DiseaseArticle{
				DiseaseID: req.DiseaseID,
				ArticleID: video,
				Type:      "video",
			})
		}

		err = tx.Create(&diseaseGraphic).Error
		return err
	})
}

func (diseaseService *DiseaseService) GetDiseaseGraphic(disease_id string) (DiseaseGraphic []healthReq.DiseaseArticle, err error) {
	err = global.GVA_DB.Where("disease_id = ?", disease_id).Find(&DiseaseGraphic).Error
	return
}
